var o = require("gameData"),
    i = require("G");
cc.Class({
    extends: cc.Component,
    properties: {
        starRoot: cc.Node,
        uiRoot: cc.Node,
        coverLayer: cc.Node,
        waitingLayer: cc.Node,
        guideLayer: cc.Node,
        goIn: cc.Node,
        win: cc.Node,
        fail: cc.Node,
        tool_destroy: cc.Node,
        tool_change: cc.Node,
        tool_reset: cc.Node,
        tool1num: cc.Label,
        tool2num: cc.Label,
        tool3num: cc.Label,
        tool1add: cc.Node,
        tool2add: cc.Node,
        tool3add: cc.Node,
        toolTips: cc.Label,
        progressBar: cc.ProgressBar,
        levelLabel: cc.Label,
        targetScoreLabel: cc.Label,
        scoreLabel: cc.Label,
        goldNode: cc.Node,
    },
    onLoad: function () {
        this.starRoot.getComponent("MatrixCtrl").init(), i.LocalStorage.init();
        // var t = cc.winSize;
        // this.ratio = t.height / t.width, this.ratio > 2.06 && (this.uiRoot.getComponent("cc.Widget").top = 100,
        //     this.starRoot.getComponent("cc.Widget").bottom = .15), this.ratio < 1.4 && (this.uiRoot.scale = 5 / 7,
        //     this.starRoot.scale = 10 / 14), this.ratio < 1.7 && this.ratio >= 1.4 && (this.starRoot.getComponent("cc.Widget").bottom = .06);
    
        //监听窗口大小变化时的回调，每次窗口变化都要自动适配
        cc.view.setResizeCallback(() => this.screenAdapter());
        this.screenAdapter();
    },

    /**
     * Fit Height 模式：适用于宽大于高的屏幕
     * Fit Width 模式：适用于高大于宽的屏幕
     */
    screenAdapter() {
        //当前屏幕分辨率比例
        let screenRatio = cc.winSize.width / cc.winSize.height;
        //设计稿分辨率比例
        let designRatio = cc.Canvas.instance.designResolution.width / cc.Canvas.instance.designResolution.height;
        
        if (screenRatio <= 1) {
            //屏幕高度大于或等于宽度,即竖屏
            if (screenRatio <= designRatio) {
                this.setFitWidth();
            } else {
                //此时屏幕比例大于设计比例
                //为了保证纵向的游戏内容不受影响，应该使用 fitHeight 模式
                this.setFitHeight();
            }
        } else {
            //屏幕宽度大于高度,即横屏
            this.setFitHeight();
        }
    },

    setFitWidth() {
        cc.Canvas.instance.fitHeight = false;
        cc.Canvas.instance.fitWidth = true;
    },

    setFitHeight() {
        cc.Canvas.instance.fitHeight = true;
        cc.Canvas.instance.fitWidth = false;
    },

    start: function () {
        this.play();
    },
    goInAnimation: function (t, e, n) {
        this.goIn.getComponent("GoIn").playAnim({
            level: t,
            targetScore: e,
            callback: function () {
                this.starRoot.getComponent("MatrixCtrl").gameStart(n, e), this.waitingLayer.active = !1;
            }.bind(this)
        });
    },
    initTitle: function (t, e, n) {
        // 初始化文本
        this.progressBar.progress = cc.misc.clamp01(n / e), 
        this.initTool(), 
        this.levelLabel.string = "关卡" + t,
        this.targetScoreLabel.string = e,
        this.scoreLabel.string = n;
    },
    initTool: function () {
        this._tool1num = i.LocalStorage.get("tool1num"),this.tool1num.string = this._tool1num;
        this._tool2num = i.LocalStorage.get("tool2num"),this.tool2num.string = this._tool2num;
        this._tool3num = i.LocalStorage.get("tool3num"),this.tool3num.string = this._tool3num;
    },
    next: function (t) {
        console.log("过关！"), this.waitingLayer.active = !0;
        //i.Interface.saveScore(t);
        t > i.LocalStorage.get("maxscore") && i.LocalStorage.set("maxscore", t); 
        var e = i.LocalStorage.get("level");
        i.LocalStorage.set("level", ++e),
        i.LocalStorage.set("score",t);
        //i.LocalStorage.set("score", t);
        var n = this.win.getComponent("Win");
        n.reward.string = "奖励 " + o.getRewardScore(o.remainingStarCount), n.remain.string = o.remainingStarCount,
            this.coverLayer.active = !0, this.win.active = !0, n.playWinAnimation(function () {
                this.coverLayer.active = !1, this.play(t), this.win.active = !1;
            }.bind(this));
    },
    over: function (t) {
        t > i.LocalStorage.get("maxscore") && i.LocalStorage.set("maxscore", t);    
        this.fail.getComponent("Fail").fail(t);
        // i.Interface.submitScore(t), t > i.LocalStorage.get("maxscore") && i.LocalStorage.set("maxscore", t),
        //     console.log("本局剩余复活次数", i.LocalStorage.get("reviveTime")), i.LocalStorage.get("reviveTime") > 0 ? (console.log("分数不够，失败！即将进入复活界面"),
        //         this.fail.getComponent("Fail").fail(t)) : (console.log("本局已无复活次数"), this.fail.getComponent("Fail").fail(t),
        //         this.fail.getComponent("Fail").skip());
    },
    onGoHome: function () {
        i.score = parseInt(this.scoreLabel.string), cc.director.emit("clickSound"), cc.director.loadScene("gameHome");
    },
    play: function () {
        var t = cc.winSize;
        t.height / t.width >= 1.7 && (console.log("播放广告"), i.Interface.showBannerAD());
        // 从上次通关记录继续
        var e = i.LocalStorage.get("level"),
            n = i.getTargetScore(e),
            a = i.LocalStorage.get("score");
        // 如果当前分数达不到上一等级的目标分数，则校正
        if(e > 1 && i.getTargetScore(e-1) > a){
            a = i.getTargetScore(e-1);
        }
        // 去掉引导
        (this.waitingLayer.active = !0,
                 0 != o.starSprite.length ? (this.initTitle(e, n, i.score), this.goInAnimation(e, n, i.score)) : (this.initTitle(e, n, a),
                     this.goInAnimation(e, n, a)));
    },
    //购买道具
    buyTool1: function () {
        var t = this;
        this.updateGold(10, function () {
            this.toolTips.string = "购买道具消耗10个星星", i.LocalStorage.set("tool1num", ++this._tool1num),
                this.tool1num.string = this._tool1num;
        }.bind(this)), setTimeout(function () {
            t.toolTips.string = "";
        }, 1500);
    },
    buyTool2: function () {
        var t = this;
        this.updateGold(10, function () {
            this.toolTips.string = "购买道具消耗10个星星", i.LocalStorage.set("tool2num", ++this._tool2num),
                this.tool2num.string = this._tool2num;
        }.bind(this)), setTimeout(function () {
            t.toolTips.string = "";
        }, 1500);
    },
    buyTool3: function () {
        var t = this;
        this.updateGold(10, function () {
            this.toolTips.string = "购买道具消耗10个星星", i.LocalStorage.set("tool3num", ++this._tool3num),
                this.tool3num.string = this._tool3num;
        }.bind(this)), setTimeout(function () {
            t.toolTips.string = "";
        }, 1500);
    },
    updateGold: function (t, e) {
        var n = i.LocalStorage.get("gold");
        return n - t >= 0 ? (i.LocalStorage.set("gold", n - t), cc.director.emit("gold"),
            e(), !0) : (console.log("金币不足"), i.Interface.hitBox("金币不足"), !1);
    },
    // 使用道具
    tool1: function () {
        this._tool1num > 0 ? (this.updateTool(1, this.tool_destroy), i.LocalStorage.set("tool1num", --this._tool1num),
                this.tool1num.string = this._tool1num) : (console.log("炸弹不足，请购买"));
    },
    tool2: function () {
        this._tool2num > 0 ? (this.updateTool(2, this.tool_change), i.LocalStorage.set("tool2num", --this._tool2num),
                this.tool2num.string = this._tool2num) : (console.log("刷子不足，请购买"));
    },
    tool3: function () {
        this._tool3num > 0 ? (this.updateTool(3, this.tool_reset), i.LocalStorage.set("tool3num", --this._tool3num),
            this.tool3num.string = this._tool3num, 0 == this._tool3num && (this.tool3add.active = !0)) 
            : (console.log("重置不足，请购买"));
    },
    updateTool: function (t, e) {
        this.starRoot.getComponent("MatrixCtrl").setToolType({
            key: t,
            callback: function () {
                console.log("道具使用完毕"), e.stopAllActions(), e.scale = 1, this.tool_destroy.getComponent("cc.Button").interactable = !0,
                    this.tool_change.getComponent("cc.Button").interactable = !0, this.tool_reset.getComponent("cc.Button").interactable = !0;
            }.bind(this)
        });
    },
    addGold : function(num){
        var n = i.LocalStorage.get("gold");
        i.LocalStorage.set("gold",num+n);
        console.log("获得金币:",num);
        this.goldNode.getComponent("Gold").add();
    }
})